/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import translator.Translator;

/**
 *
 * @author Luuk
 */
public class ClientGUI {
    private JFrame f = new JFrame("Hanze translate");
    
    private JButton toEnglish = new JButton("Dutch to English");
    private JButton toDutch = new JButton("English to Dutch");
    private JTextField dutchText = new JTextField(10);
    private JTextField englishText = new JTextField(10);
    
    private JPanel pnlSouth = new JPanel();
    private JPanel pnlEast = new JPanel();
    private JPanel pnlWest = new JPanel();
    private JPanel pnlNorth = new JPanel();
    
    /**
     * Class that creates a graphical user interface for the client to translate words.
     */
    public ClientGUI(){
        f.getContentPane().setLayout(new BorderLayout());
        
        pnlNorth.add(toEnglish);
        pnlSouth.add(toDutch);
        
        pnlEast.add(englishText);
        pnlWest.add(dutchText);
        
        f.getContentPane().add(pnlNorth, BorderLayout.NORTH);
        f.getContentPane().add(pnlSouth, BorderLayout.SOUTH);
        f.getContentPane().add(pnlEast, BorderLayout.EAST);
        f.getContentPane().add(pnlWest, BorderLayout.WEST);
        
        //Add a actionlistener to the translate button for cliokhandling.
        toEnglish.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(dutchText.getText() != null && !dutchText.getText().isEmpty()){
                    String text = "translate:dutch>english;string:" + dutchText.getText();
                    String translated = TranslatorClient.translate(text);
                    englishText.setText(translated);
                }else{
                    alert("No word(s) specified.");
                }
            }
        } );
        
        //Add a actionlistener to the translate button for cliokhandling.
        toDutch.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(englishText.getText() != null && !englishText.getText().isEmpty()){
                    String text = "translate:english>dutch;string:" + englishText.getText();
                    String translated = TranslatorClient.translate(text);
                    dutchText.setText(translated);
                }else{
                    alert("No word(s) specified.");
                }
            }
        } );
    }
    
    /**
     * Launch method that packs and shows the GUI.
     */
    public void Launch(){
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack(); //Adjusts panel to components for display
        f.setVisible(true);
    }
    
    /**
     * Shows a message to inform the client user.
     */
    public void alert(String message){
        JOptionPane.showMessageDialog(f, message);
    }
}
